package com.czmini.cztest.controller;

import com.alibaba.fastjson.JSONObject;
import com.czmini.cztest.common.TokenDb;
import com.czmini.cztest.config.UserBaseStr;
import com.czmini.cztest.dto.PageTableRequest;
import com.czmini.cztest.dto.PageTableResponse;
import com.czmini.cztest.dto.ResultDto;
import com.czmini.cztest.dto.TokenDto;
import com.czmini.cztest.dto.testcase.QueryZcTestTaskCaseRelDeatilDto;
import com.czmini.cztest.dto.testcase.ZcTestTaskCaseRelDeatilDto;
import com.czmini.cztest.service.ZcTestTaskCaseRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.controller
 * @ClassName: ZcTestTestCaseRelController
 * @Author: 还是那个橙子
 * @Description: 任务与测试用例关联管理
 * @Date: 2022/5/14 17:22
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "zctest-任务与测试用例关联管理")
@RestController
@RequestMapping("/taskCaseRel")
public class ZcTestTaskCaseRelController {

    @Autowired
    private ZcTestTaskCaseRelService zcTestTaskCaseRelService;

    @Autowired
    private TokenDb tokenDb;

    @GetMapping("/getTaskCaseRel")
    @ApiOperation(value = "列表查询")
    public ResultDto<PageTableResponse<ZcTestTaskCaseRelDeatilDto>> list(HttpServletRequest request,
                                                                         PageTableRequest<QueryZcTestTaskCaseRelDeatilDto> pageTableRequest) {
        log.info("获取任务与测试用例关联列表 入参：{}", JSONObject.toJSONString(pageTableRequest));
        if (Objects.isNull(pageTableRequest)){
            return ResultDto.success("列表查询参数不能为空");
        }

        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        //查询请求参数
        QueryZcTestTaskCaseRelDeatilDto params = pageTableRequest.getParams();

        if (Objects.isNull(params)) {
            params=new QueryZcTestTaskCaseRelDeatilDto();
        }
        //赋值
        params.setCreateUserId(tokenDto.getUserId());
        //赋值到请求参数中
        pageTableRequest.setParams(params);

        ResultDto<PageTableResponse<ZcTestTaskCaseRelDeatilDto>> responseResultDto = zcTestTaskCaseRelService.listDeatil(pageTableRequest);

        return responseResultDto;
    }


}
